package com.minelx.statementplus.ui.form.swing;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

import static javax.swing.JOptionPane.QUESTION_MESSAGE;

public class DialogInputs {
	public static <R> R chooseOneOf(Window owner, R[] elements, String prompt) {
		if (elements.length == 0) {
			throw new IllegalArgumentException("empty elements.");
		}
		return (R) Objects.requireNonNull(JOptionPane.showInputDialog(owner, prompt, prompt, QUESTION_MESSAGE, null, elements, elements[0]));
	}

	public static String candyOrJoke(String prompt) {
		return candyOrJoke(null, prompt);
	}

	public static String candyOrJoke(Window context, String prompt) {
		return Objects.requireNonNull(JOptionPane.showInputDialog(context, prompt));
	}

	public static String gimme(Window context, String prompt) {
		while (true) {
			String result = JOptionPane.showInputDialog(context, prompt);
			if (result != null) {
				return result;
			}
		}
	}

	public static String candyOrDrink(Window context, String drink, String prompt) {
		String result = JOptionPane.showInputDialog(context, prompt);
		if (result != null) {
			return result;
		}
		return drink;
	}
}
